<?php
/**
 * File htdocs/Aplication/Models/updateque.php ini berisi class Updateque
 *
 * PHP version 5.3
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
ini_set("display_errors", 1);


/**
 * pengisian form :
 * tambah :
 * 1. pilih mau nambah anak ato pasangan...
 * 2. pilih pasangan/anaknya siapa....
 * 3. isi form...
 * 4. upload foto... boleh di skip...
 * 5. view bio...
 * 6. close...
 * 
 *  
 *  edit :
 *  1. lansung klik di nama
 *  2. tampil view bio...
 *  3. edit or close...
 *  4. edit : isi form...
 *  5 view bio...
 */

/**
 * Class updateque merupakan class yang berisi perintah-perintah SQL 
 * yang digunakan untuk update.
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */
class home extends CI_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper("html");
		$this->load->helper("form");
		$this->load->helper("url");
		
		$this->load->library("session");
		$this->load->model("home_model", "homeModel");

	}
	
	public function index()
	{
		$sesDat = $this->session->userdata("id", true);
		if ($sesDat == false) {
			redirect("/home/login", "refresh");
		}
		
		$openData = array (
			'ctlTitle' => 'Pohon Keluarga Ratar',
			'ctlScript' => '
				<link rel="stylesheet" type="text/css"
				 href="' . base_url() . 'css/layout.css" />
		 		<link rel="stylesheet" type="text/css"
				 href="' . base_url() . 'css/form.css" />
				<link rel="stylesheet" type="text/css"
				 href="' . base_url() . 'css/tree.css" />
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.cycle.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/home.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/form.js">
				</script>
				'
		);
		
		
		$this->_retrieve_tree();
		
		$this->load->view('open', $openData);	
		
		$tree = $this->_retrieve_tree();
		
		$arrHome = array (
		    'ctlId' => $sesDat, 
			'ctlTree' => $tree 
		);
		
		$this->load->view('home/view_home', $arrHome);
	}
	
	public function login()
	{
		$this->_check_login();
		
		$sesDat = $this->session->userdata("id", true);
		if ($sesDat) {
			redirect("/home", "refresh");
		}
		
		$openData = array (
			'ctlTitle' => 'Login | keluargaRatar.com',
			'ctlScript' => '
		 		<link rel="stylesheet" type="text/css"
				 href="' . base_url() . 'css/login.css" />
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.js">
				</script>
				<script type="text/javascript"
				 src="' . base_url() . 'javascript/jquery.cycle.js">
				</script>

				'
		);
		
		$this->load->view('open', $openData);
		
		
		$this->load->view('view_login');
	}
	
	private function _check_login()
	{
		$isSubmit = $this->input->post("signin", true);
		
		$username = "";
		if ($isSubmit) {
			$username = $this->input->post("username", true);
			$password = $this->input->post("password", true);
			
			$arrSelect = array(
				'username' => $username,
				'password' => md5($password)
			);
			
			$result = $this->homeModel->select_user($arrSelect);
			
			if (count($result)>0) {
				$this->session->set_userdata(
					"id", $result[0]['id_user']
				);
			}
		}
		
		return $username;
	}
	
	public function logout()
	{
		$this->session->sess_destroy();
		redirect("/home/login", "refresh");
	}
	
	public function setting($id = "")
	{
	    
		$sesDat = $this->session->userdata("id", true);
		if ($sesDat == false) {
			redirect("/home/login", "refresh");
		}
		
		$msg = $this->_submit_setting();
		
	    if ($id != "") {
	        
    		$openData = array (
    			'ctlTitle' => 'Pohon Keluarga Ratar',
    			'ctlScript' => '
    		 		<link rel="stylesheet" type="text/css"
    				 href="' . base_url() . 'css/login.css" />
    				<script type="text/javascript"
    				 src="' . base_url() . 'javascript/jquery.js">
    				</script>
    				<script type="text/javascript"
    				 src="' . base_url() . 'javascript/jquery.cycle.js">
    				</script>
    
    				'
    		);
    		
    		$this->load->view('open', $openData);
    		
	        $arrSelect = array(
				'id_user' => $id
			);
			
			$result = $this->homeModel->select_user($arrSelect);
			
			$arrSetting = array (
			    'ctlId' => $id
			);
			
			$this->load->view("home/view_setting", $arrSetting);
	    }
			
	}
	
	private function _submit_setting() {
	    $isSubmit = $this->input->post("update", true);
	    
	    $msg = "";
	    
	    if ($isSubmit) {
    		$oldPwd = $this->input->post("oldPwd", true);
    		$newPwd = $this->input->post("newPwd", true);
    		$confPwd = $this->input->post("newPwdConf", true);
    		$id = $this->input->post("id", true);
    		
    		if ($newPwd == $confPwd) {
    		    $arrSelect = array(
					'id_user' => $id
    			);
    			
    			$result = $this->homeModel->select_user($arrSelect);
    			
    			if (md5($oldPwd) == $result[0]["password"]) {
    			    $result = $this->homeModel->update_password($id, $newPwd);
    			    
    			    if ($result) {
    			        $msg = "password berhasil diganti";
    			    } else {
    			        $msg = "password tidak dapat diganti cobalah beberapa saat lagi";
    			    }
    			} else {
    			    $msg = "password lama tidak cocok";
    			}
    		} else {
    		    $msg = "password baru tidak sama dengan field konfirmasi";
    		}
    		
	    }

	    return $msg;
	}
	
	private function _retrieve_tree()
	{
	    $root = $this->_retrieve_root();
	    //print_r($root);
	    $htmlTree = $this->_rec_tree($root, 0);
	    
	    return $htmlTree;
	}
	
	private function _rec_tree($arrData, $level)
	{
	    $level++;
	    
	    $arrCouple = $this->_retrieve_couple($arrData['id_data_diri'], $arrData);
	    
	    if (!empty($arrCouple)) {
	        $arrChild = $this->_retrieve_child($arrCouple['id_nikah']);
	        
	        $arrReady = array();
	        
	        foreach ($arrChild as $key => $arrVal) {
	            $arrReady[] = $this->_rec_tree($arrVal, $level);
	        }
	        
	        $ulChild = "";
	        
	        if (!empty($arrReady)) {
	            $ulChild = ul($arrReady);
	        }
	        
	        
	        $arrDet = array (
	            'name' => 'member',
    		    'class' => 'viewBio trunk level'.$level ,
    		    'value' => $arrCouple['arrTrunk']['id_data_diri'],
    		    'content' => $arrCouple['arrTrunk']['nama']
	        );
	        
	        $btnTrunk = form_button($arrDet);
	        
	        $arrDet = array (
	            'name' => 'member',
    		    'class' => 'viewBio branch level'.$level ,
    		    'value' => $arrCouple['arrBranch']['id_data_diri'],
    		    'content' => $arrCouple['arrBranch']['nama']
	        );
	        
	        $btnBranch = form_button($arrDet);
	        
	        $strParent = "<div>" . $btnTrunk . " & " . 
	            $btnBranch . "</div>";
	        
	        return $strParent . $ulChild;
	    } else {
	        $arrDet = array (
	            'name' => 'member',
    		    'class' => 'viewBio trunk level'.$level ,
    		    'value' => $arrData['id_data_diri'],
    		    'content' => $arrData['nama']
	        );
	        
	        $button = form_button($arrDet);
	        
	        return "<div>" . $button ."</div>";
	    }
	    
	}
	
	private function _retrieve_couple($id, $arrData)
	{
	    $result = $this->homeModel->retrieve_couple($id, $arrData);
	    
	    if (!empty($result)) {
	        $idNikah = $result[0]['id_nikah'];
    	    unset($result[0]['id_nikah']);
    	    return array (
    	        'id_nikah' => $idNikah,
    	        'arrTrunk' => $arrData,
    	        'arrBranch' => $result[0]
    	    );
	    } else {
	        return array();
	    }
	}
	
	private function _retrieve_root() 
	{
	    $result = $this->homeModel->retrieve_root();
	    
	    return $result[0];
	}
	
	private function _retrieve_child($idMerid)
	{
	    $result = $this->homeModel->retrieve_child($idMerid);
	    
	    return $result;
	}
}